define(function (require, exports, module) {
	var SWFUpload = require('swfu');
	var FileProgress = require('FileProgress');
	var $ = require('$');
	var json = require('json');

	exports.att_show = function(serverData, file) {
		var data = json.parse(serverData);
		var error = data.error;
		if (error) {
			alert(data.msg);
			return false;
		}
		var id = data.id;
		var src = data.url;
		var ext = data.extension;
		var filename = data.basename;

		var img;

		if ( ext == 1 ) {
			img = '<a href="javascript:;" data-cancelId ="' + id + '" class="on"><div class="icon"></div><img src="' + src + '" width="80" imgid="' + id + '" path="' + src + '" title="' + filename + '"/></a>';
		} else {
			img = '<a href="javascript:;" data-cancelId ="' + id + '" class="on"><div class="icon"></div><img src="statics/images/ext/' + ext + '.png" width="80" imgid="' + id + '" path="' + src + '" title="' + filename + '"/></a>';
		}
		$('#fsUploadProgress').append('<li><div id="attachment_' + id + '" class="img-wrap"></div></li>');
		$('#attachment_' + id).html(img);
		$('#att-status').append('|' + src);
		$('#att-name').append('|' + filename);
	};

	exports.att_insert = function (obj, id) {
		var uploadfile = $("#attachment_" + id + "> img").attr('path');
		$('#att-status').append('|' + uploadfile);
	};

	exports.att_cancel = function(obj, id, source) {
		var src = $(obj).children("img").attr("path");
		var filename = $(obj).children("img").attr("title");
		if ( $(obj).hasClass('on') ) {
			$(obj).removeClass("on");
			var imgstr = $("#att-status").html();
			var length = $("a[class='on']").children("img").length;
			var strs = filenames = '';
			for (var i = 0; i < length; i++) {
				strs += '|' + $("a[class='on']").children("img").eq(i).attr('path');
				filenames += '|' + $("a[class='on']").children("img").eq(i).attr('title');
			}
			$('#att-status').html(strs);
			$('#att-name').html(filenames);
			if ( source == 'upload' ) $('#att-status-del').append('|' + id);
		} else {
			$(obj).addClass("on");
			$('#att-status').append('|' + src);
			$('#att-name').append('|' + filename);
			var imgstr_del = $("#att-status-del").html();
			var imgstr_del_obj = $("a[class!='on']").children("img")
			var length_del = imgstr_del_obj.length;
			var strs_del = '';
			for (var i = 0; i < length_del; i++) {
				strs_del += '|' + imgstr_del_obj.eq(i).attr('imgid');
			}
			if ( source == 'upload' ) $('#att-status-del').html(strs_del);
		}
	}


	//swfupload functions
	exports.fileDialogStart = function(){
		/* I don't need to do anything here */
	};

	exports.fileQueued = function(file) {
		if ( file != null ) {
			try {
				var progress = new FileProgress(file, this.customSettings.progressTarget);
				progress.toggleCancel(true, this);
			} catch (ex) {
				this.debug(ex);
			}
		}
	}

	exports.fileDialogComplete = function(numFilesSelected, numFilesQueued) {
		try {
			if ( this.getStats().files_queued > 0 ) {
				document.getElementById(this.customSettings.cancelButtonId).disabled = false;
			}

			/* I want auto start and I can do that here */
			//this.startUpload();
		} catch (ex) {
			this.debug(ex);
		}
	}

	exports.uploadStart = function (file) {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus("正在上传请稍后...");
		return true;
	};

	exports.uploadProgress = function (file, bytesLoaded, bytesTotal) {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setProgress(percent);
		progress.setStatus("正在上传(" + percent + " %)请稍后...");
	};

	exports.uploadSuccess = function (file, serverData) {
		exports.att_show(serverData, file);
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setComplete();
		progress.setStatus("文件上传成功");
	};

	exports.uploadComplete = function (file) {
		if ( this.getStats().files_queued > 0 ) {
			this.startUpload();
		}
	};

	exports.uploadError = function (file, errorCode, message) {
		var msg;
		switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				msg = "上传错误: " + message;
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				msg = "上传错误";
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				msg = "服务器 I/O 错误";
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				msg = "服务器安全认证错误";
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
				msg = "附件安全检测失败，上传终止";
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				msg = '上传取消';
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				msg = '上传终止';
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				msg = '单次上传文件数超出限制！';
				break;
			default:
				msg = message;
				break;
		}
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setError();
		progress.setStatus(msg);
	};

	exports.fileQueueError = function (file, errorCode, message) {
		var errormsg;
		switch (errorCode) {
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				errormsg = "请不要上传空文件";
				break;
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				errormsg = "队列文件数量超过设定值";
				break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				errormsg = "文件尺寸超过设定值";
				break;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				errormsg = "文件类型不合法";
			default:
				errormsg = '上传错误，请与管理员联系！';
				break;
		}

		var progress = new FileProgress('file', this.customSettings.progressTarget);
		progress.setError();
		progress.setStatus(errormsg);

	}
});



